import { getChannels, getAllChannels } from '@/api/channels'

const state = {
  userChannel: [],
  allChannel: []
}
const mutations = {
  setUserChannel(state, value) {
    state.userChannel = value
  },
  setAllChannel(state, value) {
    state.allChannel = value
  },
  // 往我的频道中添加频道
  addUserChannel(state, channel) {
    state.userChannel.push(channel)
  }
}
const actions = {
  async ActionGetUserChannel(context) {
    const res = await getChannels()
    console.log('获取用户频道', res)
    context.commit('setUserChannel', res.data.channels)
  },
  async ActionGetAllChannel(context) {
    const res = await getAllChannels()
    console.log('获取全部频道', res)
    context.commit('setAllChannel', res.data.channels)
  }
}
const getters = {
  optionChannel(state) {
    return state.allChannel.filter(v => {
      // const res = state.userChannel.some(item => item.id === v.id)
      // if (res === true) {
      //   return false
      // } else {
      //   return true
      // }
      return !state.userChannel.some(item => item.id === v.id)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
